package com.gzjh_group.photoalbum;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.PhotoView;

import butterknife.BindView;

/**
 * 相册图片适配器
 */
public class PhotoPagerAdapter extends PagerAdapter implements OnPhotoTapListener {

    private GirlImage girlImage;
    private LayoutInflater layoutInflater;
    private Context context;
    private OnPhotoTapListener onPhotoTapListener;
    private OnLongClickListener onLongClickListener;
    private ImageLoader imageLoader;

    public PhotoPagerAdapter(GirlImage girlImage, Context context) {
        this.girlImage = girlImage;
        this.context = context;
        if (layoutInflater == null) {
            layoutInflater = LayoutInflater.from(context);
        }
    }

    @Override
    public int getCount() {
        return girlImage.getResults().size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        View rootView = layoutInflater.inflate(R.layout.item_activity_photo, null);
        PhotoView photoView = rootView.findViewById(R.id.iv_photo);
        if (imageLoader != null) {
            imageLoader.loadImage(girlImage.getResults().get(position).getUrl(), photoView);
        }
        container.addView(rootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        photoView.setOnPhotoTapListener(this);
        photoView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onLongClickListener.onLongClick(v, position);
                return false;
            }
        });

        return rootView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public void onPhotoTap(ImageView view, float x, float y) {
        onPhotoTapListener.onPhotoTap(view, x, y);
    }

    public interface OnLongClickListener {
        void onLongClick(View view, int position);
    }

    public interface OnPhotoTapListener {
        void onPhotoTap(View view, float x, float y);
    }

    public interface ImageLoader {
        void loadImage(String url, ImageView imageView);
    }

    public void setOnPhotoTapListener(OnPhotoTapListener onPhotoTapListener) {
        this.onPhotoTapListener = onPhotoTapListener;
    }

    public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
    }

    public void setImageLoader(ImageLoader imageLoader) {
        this.imageLoader = imageLoader;
    }

}
